<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,viewport-fit=cover,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no">
    <title>会诊申请单</title>
    <script src="js/rem.js"></script>
    <link rel="stylesheet" href="css/weui.min.css">
    <link rel="stylesheet" href="css/jquery-weui.min.css">
    <link rel="stylesheet" href="css/public.css">
    <link rel="stylesheet" href="css/index.css">
    <script>(function() {var _53code = document.createElement("script");_53code.src = "https://tb.53kf.com/code/code/96d8f960b261e49515450f8953b044bc5/1";var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(_53code, s);})();</script>
</head>
<body>
    <div class="pub-container consultation-apply bgwhite">
        <div class="top plr15">
            <h4 class="title">填写会诊申请单</h4>
            <p class="tip">* 如实全面的填写会诊信息，有助于尽快预约到合适的会诊专家</p>
        </div>
        <div class="pub-box space-border plr15">
            <span class="desc">咨询日期</span>
            <span class="info" id="nowDate"></span>
        </div>
        <form action="#" method="post" onsubmit="submitApply();return false;" autocomplete="off" id="applyForm">
            <div class="pub-box space-border plr15">
                <span class="desc"><i class="i-required">*</i><span class="last-text-justify">患者姓名</span></span>
                <input class="pub-text" type="text" name="patientName">
            </div>
            <div class="pub-box space-border plr15 sex-box" >
                <span class="desc"><i class="i-required">*</i><span class="last-text-justify">性别</span></span>
                <div>
                    <span id="sexView">男</span>
                    <span class="right-arrow-btn"></span>
                </div>
                <input class="pub-picker-input" type="text" name="patientSex" value="1">
            </div>
            <div class="pub-box space-border plr15">
                <span class="desc"><i class="i-required">*</i><span class="last-text-justify">年龄</span></span>
                <input class="pub-text" type="text" name="patientAge">
            </div>
            <div class="pub-box space-border plr15">
                <span class="desc"><i class="i-required">*</i><span class="last-text-justify">咨询目的</span></span>
                <textarea class="pub-textarea" name="consultMessage" rows="4" id="purposeTextarea"></textarea>
            </div>
            <div class="phone-box" id="phoneBox">
                
            </div>
            <div class="submit-box">
                <p class="tip">*提交申请后会有您的专属客服为您解答会诊相关疑问</p>
                <input class="pub-submit-btn" type="submit" value="提交申请">
            </div>
        </form>
    </div>
    <script src="js/jquery-3.6.0.min.js"></script>
    <script src="js/jquery-weui.min.js"></script>
    <script src="js/md5.js"></script>
    <script src="js/public.js"></script>
    <script>
        $('#purposeTextarea').on('keypress', function(e){
            if(e.keyCode == 13) return false;
        })
        var token = getCookie('token');
        $('[name=patientAge]').on('input', function(e){
            var value = $.trim($(this).val());
            value = value.replace(/[^\d]/, '').replace(/((1\d{0,2})|([23456789]\d{0,1})).*/, '$1');
            $(this).val(value);
        })

        // 如未登录，显示手机号、验证码输入框
        if(!token){
            $('#phoneBox').html('<div class="pub-box space-border plr15">' + 
                '<span class="desc"><i class="i-required">*</i><span class="last-text-justify">患者电话</span></span>' + 
                '<input class="pub-text" type="text" name="patientPhone">' + 
                '<span class="get-code-btn" id="getCodeBth">获取验证码</span>' + 
            '</div>' + 
            '<div class="pub-box space-border plr15">' + 
                '<span class="desc"><i class="i-required">*</i><span class="last-text-justify">手机验证码</span></span>' + 
                '<input class="pub-text" type="text" name="checkCode">' + 
            '</div>');

            // 获取验证码
            var sendStatus = false;
            $('#getCodeBth').click(function(){
                if(sendStatus) return;
                var phone = $.trim($('[name=patientPhone]').val());
                // 数据验证
                if(validEmpty(phone, '患者电话不能为空')) return;
                if(validPhone(phone)) return;

                sendStatus = true;
                getCode($(this), phone, 'apply')
            })
        }else {
            $('#phoneBox').html('');
        }

        $('[name=consultMessage]').val(sessionStorage.getItem('consultCont'));

        // 写入当前日期
        $('#nowDate').html(format1(new Date()));

        // 选择性别
        $('.sex-box').click(function(){
            $('input,textarea').blur();
        })
        $('[name=patientSex]').picker({
            title: "请选择您的性别",
            cols: [{
                textAlign: 'center',
                displayValues: ['男', '女'],
                values: [1, 0]
            }],
            onChange: function(e){
                $('#sexView').html(e.cols[0].displayValue);
            }
        });

        // 提交会诊申请单
        // 数据验证
        function dataValidation(data){
            var msg = {
                patientName: '患者姓名不能为空',
                patientSex: '请选择性别',
                patientAge: '年龄不能为空',
                consultMessage: '咨询目的不能为空'
            };
            if(!token){
                msg.patientPhone = '患者电话不能为空';
                msg.checkCode = '验证码不能为空';
            }
            for(var k in msg){
                if(validEmpty(data[k], msg[k])) return true;
            }

            if(!token && validPhone(data.patientPhone, '手机号格式不正确')) return true;
            return false;
        }
        // 提交
        var subState = false; // 防重
        function submitApply(){
            if(subState) return false;
            var data = $('#applyForm').serializeObject();
            if(dataValidation(data)) return false;
            subState = true
            // 提交申请
            if(token){
                data.userId = getCookie('userId');
                data.token = getCookie('token');
                data.patientPhone = localStorage.getItem('mobile');
            }
            http('/consulation/saveMessage', {
                loading: true,
                data: data,
                showAlert: true,
                success: function(res){
                    if(res.code === '01'){
                        toast('提交成功');
                        if(!token){
                            setCookie('token', res.data.token);
                            setCookie('userId', res.data.userId, 1);
                            localStorage.setItem("mobile", data.patientPhone);
                        }
                        // backIndex(1000);
                        setTimeout(function(){
                            window.location.replace(customUrl);
                        }, 1000)
                    }
                },
                complete: function(){
                    subState = false;
                }
            })
            return false;
        }
    </script>
</body>
</html>